from flask import Flask, send_from_directory
from comm.functons import config
from flask_cors import CORS
import time
import os

app = Flask(
    __name__,
    static_folder=config('base_dir'),
    static_url_path='',
    template_folder="templates"
)
CORS(app, supports_credentials=True)


@app.route('/html/<path:filename>')
def html(filename):
    return send_from_directory('resources/html/', filename)


@app.route('/resources/imgs/<path:filename>')
def resources(filename):
    for _ in range(5):
        full_path = os.path.join(config('gua_img_dir'), filename)
        if os.path.exists(full_path):
            return send_from_directory('resources/imgs', filename)
        time.sleep(1)
    return send_from_directory('resources/imgs', filename)


if '__main__' == __name__:
    from webapp.index import index
    from webapp.bus import bus

    from webapp.gua import gua

    app.register_blueprint(index)
    app.register_blueprint(bus)

    app.register_blueprint(gua)
    app.run(host='0.0.0.0')
